begin
i=0
si=i
nyear_chunks=17
nyears=165
temp_means = new(nyears,float)
;nfiles=90
cs=0
csn=(/"cmip6","smbb"/)
comps=(/1001,1231,1251,1281,1301/)
comp=0
cruns=10
data  = new( nfiles, "string")
csv_filename = "./cesm2_ts_"+sprinti("%0.4i", i)+"-"+sprinti("%0.4i", nfiles+si-1)+".csv"
do while (i.lt.nfiles+si)
    runnum=i%10 +1
    compnum=i /10
    if (i.lt.50)
        cs=0
        if (compnum.eq.0)
            comp=comps(compnum)+(runnum-1)*20
        else
            comp=comps(compnum)
        end if
    else
	cs=1
	runnum=runnum+10
	comp=comps(compnum-4)
    end if
    chunk=0
    temp_means(0:nyears-1)=0
    casepart="b.e21.BHIST"+ csn(cs) +".f09_g17.LE2-"\
             +tostring(comp) +"."+ tostring_with_format(runnum, "%3.3d")+ ".cam.h0.TS."
    print("+"+casepart)
    do while (chunk.lt.nyear_chunks)
        syr=1850+10*chunk
	if (chunk.le.15)
	    eyr=syr+9
	else
	    eyr=syr+4
	end if
        casename="./b.e21/"+casepart+tostring(syr)+"01-"+tostring(eyr)+"12.nc"
       ; print("+"+casename)
        if (fileexists(casename)) then
        f = addfile(casename,"r") 
        gw   = f->gw
        if (chunk.le.15) 
            TS= f->TS
            tmp = wgt_areaave(TS, gw, 1.0, 1)
            tmp_means= month_to_annual(tmp, 1)
	    temp_means(chunk*10:chunk*10+9)=tmp_means(0:9)
	else
            TS2= f->TS
            tmp2 = wgt_areaave(TS2, gw, 1.0, 1)
            tmp2_means= month_to_annual(tmp2, 1)
	    temp_means(chunk*10:chunk*10+4)=tmp2_means(0:4)
        end if
        end if
        chunk=chunk+1
    end do
    data(i-si) = casename
    do nt=0,nyears-1
        data(i-si) = data(i-si) +","+ sprintf("%10.3f ", temp_means(nt))
    end do
    print( sprinti("%0.4i", i) )
    i= i+1
end do
    ;write_table("example1.txt", "w", alist, "%s")
    asciiwrite (csv_filename , data)
end
